package com.control.qct.businesssteward.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.GoodsBrandsAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.BrandModel;
import com.control.qct.businesssteward.utils.EditTextUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 获取品牌
 * Created by Administrator on 2017/12/19 0019.
 */

public class GoodsBrandsActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, HttpUtils
        .IHttpResult {
    @BindView(R.id.et_search)
    EditText mEtSearch;
    @BindView(R.id.iv_delete)
    ImageView mIvDelete;
    @BindView(R.id.tv_search)
    TextView mTvSearch;
    @BindView(R.id.rv_goods_measure_unit)
    RecyclerView mRvGoodsMeasureUnit;
    private GoodsBrandsAdapter mGoodsBrandsAdapter;
    private int mBrandSN;
    private int page = 1;
    private final int row = 20;
    private List<BrandModel.DatasBean> mBrandModelList;//品牌列表
    private String title;

    public GoodsBrandsActivity() {
        super(R.layout.fragment_goods_measure_unit);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mBrandSN = extras.getInt("brandSN");
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("品牌选择");
        mBrandModelList = new ArrayList<>();
        getBrands(true, "");
        mRvGoodsMeasureUnit.setLayoutManager(new LinearLayoutManager(this));
        mGoodsBrandsAdapter = new GoodsBrandsAdapter(mBrandModelList);
        mRvGoodsMeasureUnit.setAdapter(mGoodsBrandsAdapter);
        mGoodsBrandsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mGoodsBrandsAdapter.setPosition(position);
                Intent intent = new Intent();
                intent.putExtra("data", ((List<BrandModel.DatasBean>) adapter.getData()).get(position).getTitle());
                intent.putExtra("brandSN", ((List<BrandModel.DatasBean>) adapter.getData()).get(position).getBrandSN());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        EditTextUtils.isEditContent(mEtSearch, mIvDelete);

    }


    @OnClick({R.id.iv_delete, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_delete:
                mEtSearch.setText("");
                break;
            case R.id.tv_search:
                page=1;
                mBrandModelList.clear();
                title = mEtSearch.getText().toString();
                getBrands(true, title);
                break;
        }
    }

    /**
     * 获取品牌
     */
    private void getBrands(boolean isShow, String title) {
        mNetProtocol.getBrands(this, isShow, title, page, row, this);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getBrands(false, title);
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        mEtSearch.setText("");
        BrandModel brandModels = (BrandModel) baseBean.getData();
        mBrandModelList.addAll(brandModels.getDatas());
        if (mGoodsBrandsAdapter != null) {
            if (brandModels.getDatas().size() > 9) {
                mGoodsBrandsAdapter.setOnLoadMoreListener(this, mRvGoodsMeasureUnit);
                mGoodsBrandsAdapter.loadMoreComplete();
            } else {
                mGoodsBrandsAdapter.loadMoreEnd();
            }
            mGoodsBrandsAdapter.notifyDataSetChanged();
            for (int i = 0; i < mBrandModelList.size(); i++) {
                if (mBrandModelList.get(i).getBrandSN() == mBrandSN) {
                    mGoodsBrandsAdapter.setPosition(i);
//                    mRvGoodsMeasureUnit.smoothScrollToPosition(i);
                }
            }
        }
    }

    @Override
    public void onFail(BaseBean baseBean) {

    }
}
